package com.shangguan.springmvc.web.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

/**
 * @ClassName: WebGlobalConfig
 * @Description:
 * @Author: 一尘
 * @Date: 2020 年 04 月 22 14:06
 * @Version 1.0
 */
@Configuration // 就是springmvc的配置  相当于spring-mvc.xml
// 放在最外部  他会默认去扫描自己所在的包以及子包内的组件
@ComponentScan({"com.shangguan.springmvc.web.controller"}) //组件扫描
@EnableWebMvc // 开启
public class WebGlobalConfig implements WebMvcConfigurer {
    // 组件扫描就不用了
    // mvc:annotation-drivern
    /*视图解析器*/
    @Bean   // 将下面的方法的返回值的对象放入到spring-mvc容器中
    public InternalResourceViewResolver viewResolver(){
        InternalResourceViewResolver irvr = new InternalResourceViewResolver();
            irvr.setViewClass(JstlView.class);
            irvr.setPrefix("/WEB-INF/jsp/");
            irvr.setSuffix(".jsp");
            return irvr;
    }
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("index").setViewName("index");
        registry.addViewController("hello").setViewName("welcome");
    }
}
